package com.welink.cmp.adapter.api.metadata;

import com.welink.cmp.domain.metadata.Firewall;
import com.welink.cmp.dto.ErrorCode;
import com.welink.cmp.dto.metadata.DestroyUpdateCmd;
import com.welink.cmp.dto.metadata.firewall.*;
import com.welink.cmp.meatdata.service.FirewallAppService;
import com.welink.common.dto.Response;
import com.welink.common.dto.SingleResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 防火墙生命周期管理API
 *
 * @author yanwei li create  2024-05-22 11:20
 */
@RestController("/api/firewall")
public class FirewallLiftCycleServiceApi {

    @Autowired
    private FirewallAppService firewallAppService;

    /**
     * [新增]创建防火墙资产
     * @param fwAddCmd 创建资产
     * @return 资产 ID
     */
    @PostMapping("/add")
    public SingleResponse<Long> addFirewall(@RequestBody CreateFirewallAddCmd fwAddCmd){
        Firewall firewall = new Firewall();
        BeanUtils.copyProperties(fwAddCmd,firewall);
        final Long id = firewallAppService.addFirewall(fwAddCmd);
        return SingleResponse.of(id);
    }

    /**
     * [修改]启动上架防火墙
     * @param launchFwUpdCmd 上架资产
     * @return  resultCode = 0 成功 非 0 即错误码和错误信息
     */
    @PostMapping("/launch")
    public Response launchFirewall(@RequestBody LaunchFirewallUpdateCmd launchFwUpdCmd){
        final Boolean b = firewallAppService.launchFirewall(launchFwUpdCmd);
        return Response.buildSuccess();
    }

    /**
     * [修改]配置防火墙基础信息
     * @param setupBaseFwUpdCmd
     * @return
     */
    @PostMapping("/setup/base")
    public Response setupFirewallBaseInfo(@RequestBody SetupFwBaseUpdateCmd setupBaseFwUpdCmd){
        final Boolean b = firewallAppService.setupFirewallBase(setupBaseFwUpdCmd);
        if (!b){
            return Response.buildFailure(ErrorCode.CommonError.E_Service_Inner_Exception,"配置防火墙基础信息发生异常");
        }
        return Response.buildSuccess();
    }
    /**
     * [修改]配置防火墙物理信息
     * @param setupFwRackUpdCmd
     * @return  resultCode = 0 成功 非 0 即错误码和错误信息
     */
    @PostMapping("/setup/rack")
    public Response setupFirewallRackInfo(@RequestBody SetupFwRackUpdateCmd setupFwRackUpdCmd){
        final Boolean b = firewallAppService.setupFirewallRack(setupFwRackUpdCmd);
        if (!b){
            return Response.buildFailure(ErrorCode.CommonError.E_Service_Inner_Exception,"配置防火墙物理信息发生异常");
        }
        return Response.buildSuccess();
    }

    /**
     * [修改]配置防火墙网络信息
     * @param setupFwNetUpdCmd 配置防火墙
     * @return  resultCode = 0 成功 非 0 即错误码和错误信息
     */
    @PostMapping("/setup/net")
    public Response setupFirewallNetInfo(@RequestBody SetupFwNetUpdateCmd setupFwNetUpdCmd){
        final Boolean b = firewallAppService.setupFirewallNet(setupFwNetUpdCmd);
        if (!b){
            return Response.buildFailure(ErrorCode.CommonError.E_Service_Inner_Exception,"配置防火墙网络信息发生异常");
        }
        return Response.buildSuccess();
    }

    /**
     * [修改]下架防火墙
     * @param shutdownUptCmd 下架防火墙
     * @return  resultCode = 0 成功 非 0 即错误码和错误信息
     */
    @PostMapping("/shutdown")
    public Response shutdownFirewall(@RequestBody ShutdownFirewallUpdateCmd shutdownUptCmd){
        final Boolean b = firewallAppService.shutdownFirewall(shutdownUptCmd);
        if (!b){
            return Response.buildFailure(ErrorCode.CommonError.E_Service_Inner_Exception,"下架防火墙发生异常");
        }
        return Response.buildSuccess();
    }

    /**
     * [修改]注销防火墙
     * @param destroyUpdCmd 下架防火墙
     * @return  resultCode = 0 成功 非 0 即错误码和错误信息
     */
    @PostMapping("/destroy")
    public Response destroyFirewall(@RequestBody DestroyUpdateCmd destroyUpdCmd){
        final Boolean b = firewallAppService.destroyFirewall(destroyUpdCmd);
        return Response.buildSuccess();
    }
}
